<!--
 * @Descripttion: 初始化参数
 * @version: 
 * @Author: xcb
 * @Date: 2021-03-16 09:48:46
 * @LastEditors: yangyongtao
 * @LastEditTime: 2023-11-28 16:59:43
-->
<template>
  <el-card class="box-card" style="margin-bottom: 20px">
    <el-form
      layout="inline"
      :inline="true"
      label-width="180px"
      class="sedFormInput"
    >
      <el-row style="width: 100%">
        <el-form-item label="计算时间步长dt(s)" name="step">
          <el-input v-model="mainData.timeData.step" />
        </el-form-item>
        <el-form-item label="时间偏置系数θ" name="offsets">
          <el-input v-model="mainData.timeData.offsets" />
        </el-form-item>
        <el-form-item label="总糙率(n)" name="roughness">
          <el-input v-model="mainData.timeData.roughness" />
        </el-form-item>
      </el-row>
      <el-row style="width: 100%">
        <el-form-item label="监测站点输出时间间隔T" name="stcdStep">
          <el-input v-model="mainData.timeData.stcdStep" suffix="小时" />
        </el-form-item>
        <el-form-item label="全域结果输出时间间隔T" name="xscdStep">
          <el-input v-model="mainData.timeData.xscdStep" suffix="小时" />
        </el-form-item>
      </el-row>
    </el-form>
  </el-card>

  <el-card class="box-card">
    <el-form
      :model="mainData.sedData"
      :inline="true"
      label-width="auto"
      class="sedFormInput"
    >
      <el-row>
        <el-form-item label="泥沙粒径级配总分组">
          <el-input
            v-for="(ele, i) in mainData.sedData['sed_num']"
            :key="i"
            v-model="mainData.sedData['sed_num'][i]"
            readonly
          />
        </el-form-item>
        <el-form-item label="悬移质泥沙组数">
          <el-input
            v-for="(ele, i) in mainData.sedData['sed_x_num']"
            :key="i"
            v-model="mainData.sedData['sed_x_num'][i]"
            readonly
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="河床分层厚度h/(m)">
          <el-input
            v-for="(ele, i) in mainData.sedData['ch']"
            :key="i"
            v-model="mainData.sedData['ch'][i]"
            readonly
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="泥沙分组粒径d(mm)">
          <el-input
            v-for="(ele, i) in mainData.sedData['sedd']"
            :key="i"
            v-model="mainData.sedData['sedd'][i]"
            readonly
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="总床沙级配(%)">
          <el-input
            v-for="(ele, i) in mainData.sedData['csp']"
            :key="i"
            v-model="mainData.sedData['csp'][i]"
            readonly
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="悬沙级配(%)">
          <el-input
            v-for="(ele, i) in mainData.sedData['ssp']"
            :key="i"
            v-model="mainData.sedData['ssp'][i]"
            readonly
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="总挟沙力系数">
          <el-input
            v-for="(ele, i) in mainData.sedData['km']"
            :key="i"
            v-model="mainData.sedData['km'][i]"
            readonly
          >
            <template #prefix>
              {{ i == 0 ? "K=" : "m=" }}
            </template>
          </el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="总冲淤系数">
          <el-input
            v-for="(ele, i) in mainData.sedData['ed']"
            :key="i"
            v-model="mainData.sedData['ed'][i]"
            readonly
          >
            <template #prefix>
              {{ i == 0 ? "e=" : i == 1 ? "d=" : "m=" }}
            </template>
          </el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="初始含沙量(kg/m³)">
          <el-input
            v-for="(ele, i) in mainData.sedData['sc']"
            :key="i"
            v-model="mainData.sedData['sc'][i]"
            readonly
          />
        </el-form-item>
      </el-row>
    </el-form>
  </el-card>
</template>

<script lang="ts">
import { ElMessage } from "element-plus";
import {
  ref,
  defineComponent,
  reactive,
  toRefs,
  inject,
  onMounted,
  Ref,
  watch,
  getCurrentInstance,
} from "vue";
import { useStore } from "vuex";
import { cloneDeep } from "lodash-es";
import { searchNstjArguments, readPreferences } from "@/request/api";

export default defineComponent({
  name: "sedimentParam",
  setup: () => {
    const store = useStore();
    const { proxy }: any = getCurrentInstance();

    const state = reactive({
      editableData: {},
      columns: [
        {
          title: "参数名",
          dataIndex: "dataName",
          slots: { customRender: "" },
          align: "center",
          width: "20%",
          customCell: tableRowClick,
        },
      ],
    });

    const mainData: any = inject("mainData");

    // 方案参数信息
    const planDataMsgForm: any = inject("planDataMsgForm");

    onMounted(() => {});

    watch(
      () => planDataMsgForm,
      (value, prevCount) => {
        proxy.throttle("sedimentParam", () => {
          getReadSediment();
          getPreferences();
        });
      },
      { deep: true }
    );

    /**
     * @description:获取初始化参数
     * @return {*}
     */
    function getReadSediment() {
      searchNstjArguments({
        id: planDataMsgForm.selectParamPlan,
        reservoir: planDataMsgForm.selectReservoir,
        Fatype: "csfa",
      }).then((res: any) => {
        for (const key in res.data) {
          mainData.sedData[key] = res.data[key].replace(/\s/g, "").split(",");
        }
      });
    }

    function getPreferences() {
      readPreferences({
        planno: planDataMsgForm.planno,
        area: planDataMsgForm.selectReservoir,
      }).then((res: any) => {
        mainData.timeData.step = res.data.dt;
        mainData.timeData.xscdStep = res.data.res_time;
        mainData.timeData.stcdStep = res.data.mon_time;
        mainData.timeData.offsets = res.data.the;
        mainData.timeData.roughness = res.data.cancon;
      });
    }

    function tableRowClick({ dataName }) {
      return {
        onDblclick: (e) => {
          const type = e.target.getAttribute("data-type");

          if (type) {
            state.editableData[dataName][type] = cloneDeep(
              mainData.sedData.csps.find((item) => item.dataName == dataName)
            )[type];
          }
        },
        onMouseleave: (e) => {
          Object.assign(
            mainData.sedData.csps.filter(
              (item) => item.dataName == dataName
            )[0],
            state.editableData[dataName]
          );

          mainData.sedData.csps.forEach((item) => {
            let arr = [];
            for (const key in item) {
              if (key != "dataName") {
                arr.push(item[key]);
              }
            }
          });

          state.editableData[dataName] = {};
        },
      };
    }

    function labelText(text) {
      switch (text) {
        case "km":
          return "总挟沙系数";
        case "sc":
          return "初始含沙量";
        case "csp":
          return "床沙颗粒级配";
        case "ssp":
          return "悬沙级配";
        case "ed":
          return "总冲淤系数";

        case "ch":
          return "泥沙分层";
        case "sed_x_num":
          return "悬沙分组";
        case "sedd":
          return "泥沙粒径级配";
        case "sed_num":
          return "泥沙粒径级配总分组";
        default:
          return text;
      }
    }

    return {
      ...toRefs(state),

      mainData,

      labelText,
    };
  },
});
</script>

<style scoped lang="less">
.sedFormInput {
  margin-bottom: 20px;
  .el-input {
    width: 100px;
    margin: 0 4px 8px 0;
  }
}
</style>
